<?php
	include_once("../Business/survey_service.php");
	include_once("../Business/survey_result_service.php");
	
	include_once("../Business/Data/Entity/survey.php");
	include_once("../Business/Data/Entity/survey_result.php");
		
	include_once("uitilities.php");
	include_once("String.php");
	
	//1500 20100823 phale
	include_once("fckeditor/fckeditor.php") ;
	$oFCKeditor = new FCKeditor('name') ;
	$oFCKeditor->BasePath = '/fckeditor/' ;
	$oFCKeditor->Value = '<p>This is some <strong>sample text</strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor</a>.</p>' ;
	//1500 20100823 phale
	
	$msgstatus = 'display:none';
	$msg = '';
	
	$name = '';
	$results = '';
	$id = get("id");
	
	$s = null;
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if($id != "") $s = survey_service::get_survey($id);
		
		
		$nr = post("nr");
		for($i=1 ; $i<=$nr ; $i++) {
			$rid = post("r".$i."id");
			
			if($rid == '0'){
				$sr = new survey_result(0, $id, post("r".$i), post("r".$i."m_min"), post("r".$i."m_max"));
				$srid = survey_result_service::insert_survey_result($sr);
			}
			else if(strlen($rid)>0 && $rid[0]=='-'){
				$rid = substr($rid, 1);
				survey_result_service::delete_survey_result($rid);
			}
			else{
				$sr = survey_result_service::get_survey_result($rid);
				$sr->survey_result_content = post("r".$i);
				$sr->mark_min =  post("r".$i."m_min");
				$sr->mark_max =  post("r".$i."m_max");
				survey_result_service::update_survey_result($sr);
				
				$srid = $rid;
				
			}
		}
		
		$msgstatus = '';
		$msg = 'Update successfully';
	}
	
	if($id != "") {
		$s = survey_service::get_survey($id);
		$name = $s->survey_name;
		
		$sr = survey_result_service::get_survey_result_list_by_survey_id($id);
		$nr = count($sr);
		for($i=1 ; $i<=$nr ; $i++) {
			$r = $sr[$i-1];
			
			$results = $results.
			'
			<dl name="r" class="Result '.($i%2==1 ? 'grey' : '').'"> 
				<dt>
					<input title="Delete this result" value="-" type="button" onclick="if(confirmDelete(\'this result\')) deleteResult(this);">  
					<span id="rTitle">r'.$i.'</span>
					<input type="text" name="r'.$i.'m_min" value="'.$r->mark_min.'" id="m_min"/> <input type="text" name="r'.$i.'m_max" value="'.$r->mark_max.'" id="m_max"/>
					<textarea title="Result content" id="rContent" type="text" name="r'.$i.'">'.encodeS($r->survey_result_content).'</textarea> 
					<input type="hidden" name="r'.$i.'id" id="idr" value="'.$r->survey_result_id.'" />
					<dd />
			</dl>';			
		}
	}
	else {
		$nr = 0;
	}
	
	

	$main_content = String::Format('
<form method="post">
<center>
<p class="label-submit">
	<input title="Edit survey" value="Edit survey" type="button" onclick="self.location.href=\'edit_survey.php?id='.get("id").'\'">
	<input value="Update new result(s)" type="submit" title="Update new result(s)">
	<input title="Add a Result" value="+r" type="button" onclick="addResult();">
</p>
</center>
<h3>Survey</h3>	
    <table class="survey">
        <tbody>
            <tr class="even">
                <td>
                    <dl>
                        <dt>Name</dt>
                    </dl>
                </td>
                <td>
                	<input type="text" name="name" value="{0}" disabled="disabled" />                
                </td>
            </tr>
        </tbody>
    </table>
<h3>Results (<span id="nResultValue">'.$nr.'</span>)</h3>	
	<input type="hidden" name="nr" value="'.$nr.'" />
	<div id="results">
	{1}
	</div>
	<center>
	<p class="label-submit">
		<input title="Edit survey" value="Edit survey" type="button" onclick="self.location.href=\'edit_survey.php?id='.get("id").'\'">	
		<input value="Update new result(s)" type="submit" title="Update new result(s)">
		<input title="Add a Result" value="+r" type="button" onclick="addResult();">
	</p>
	</center>
	
</form>
<script>
	function updateResultInfo() {
		$("dl[name=r]").each(function(index) {
			$(this).find("textarea[id=rContent]").attr("name" , "r" + (index + 1));
			$(this).find("input[id=idr]").attr("name" , "r" + (index + 1) + "id");
			
			$(this).find("input[id=m_min]").attr("name" , "r" + (index + 1) + "m_min");
			$(this).find("input[id=m_max]").attr("name" , "r" + (index + 1) + "m_max");
		});
		
		$("dl:visible[name=r]").each(function(index) {
			$(this).find("span[id=rTitle]").html("r" + (index + 1));
			
			if(index % 2 == 1) $(this).addClass("grey");
			else $(this).removeClass("grey");
		});
		
		var vcount = $("dl:visible[name=r]").length;
		var count = $("dl[name=r]").length;
		$("#nResultValue").html(vcount ? vcount : "0") ;
		$("input[name=nr]").val(count ? count : "0") ;
		
	}

	function addResult(){
		$n = $("<dl name=\"r\" class=\"Result\" style=\"display:none\"> \
			<dt>\
				<input title=\"Delete this Result\" value=\"-\" type=\"button\" onclick=\"if(confirmDelete(\'this result\'))  deleteResult(this);\">  \
				<span id=\"rTitle\" /> \
				<input type=\"text\" id=\"m_min\" value=\"0\" /> <input type=\"text\" id=\"m_max\" value=\"0\" /> \
				<textarea title=\"Result content\" id=\"rContent\" type=\"text\"></textarea> \
				<input type=\"hidden\" name=\"\" id=\"idr\" value=\"0\" /> \
			<dd />\
		</dl>");
		
		$("#results").append($n);
		$n.slideDown(600, function(){
			updateResultInfo();
		});
	}
	
	function deleteResult(obj){
	
		var dlNameR = $(obj).parent().parent();
		dlNameR.slideUp(600,function () {
			var idr = dlNameR.find("input[id=idr]");
			if(idr.val()=="0"){
				dlNameR.remove();
			}
			else {
				var value = idr.val();
				idr.val(-value);
				dlNameR.hide();
			}
			updateResultInfo();
		});
	}

</script>
',encodeS($name), $results);
	include_once("masters/pages.php");
?> 